<?php 
class FleetAdmin
{
	public $Commander;
	public $SiteId;
	public $AjaxRun;
	public $Fleet;
	public $CurrentProductId;
	
	public function FleetAdmin()
	{
		global $commander;
		global $fleet;
		$this->Commander = $commander;
		self::setSiteId($commander->SiteId);
		if(defined('AJAX_RUN'))
		{ self::setAjaxRun(true); }
		else
		{ self::setAjaxRun(false); }
		self::setFleet($fleet);
		self::runPreActions();
		if(is_object($fleet))
		{ $fleet->loadShipList(); }
		self::runPostActions();
	}
	
	/* Get Functions */ 
	public function getSiteId() { return $this->SiteId; }
	public function getAjaxRun() { return $this->AjaxRun; }
	public function getFleet() { return $this->Fleet; }
	
	/* Set Functions */ 
	public function setSiteId($val) { $this->SiteId=$val; }
	public function setAjaxRun($val) { $this->AjaxRun=$val; }
	public function setFleet($val) { $this->Fleet=$val; }
	
	public function runPreActions()
	{
		global $fleet;
		global $commander;
		if(intval($_REQUEST['ship']) > 0)
		{ 
			$fleet->setCurrentShipId(intval($_REQUEST['ship'])); 
			$fleet->setView('commander_detail'); 
		}
		
		switch($_POST['action'])
		{
			case 'fleet_list':
				if(($_POST['new_ship_name'] != '') || ($_POST['new_ship_identity_number'] != ''))
				{ $fleet->addShip($_POST['new_ship_name'], $_POST['new_ship_identity_number']); }
				if($_POST['up'] > 0)
				{ $commander->setTableItemOrderUp($_POST['up'], 'fleet_ship', 'ship_order', 'parent_id'); }
				if($_POST['down'] > 0)
				{ $commander->setTableItemOrderDown($_POST['down'], 'fleet_ship', 'ship_order', 'parent_id'); }
				return;
			break;
			case 'editor_ship':
				if($_POST['new_schitem_port'] != '')
				{ 
					$eta = trim($_POST['new_schitem_eta']);
					$etd = trim($_POST['new_schitem_etd']);
					$etah = intval($_POST['new_schitem_etah']);
					$etam = intval($_POST['new_schitem_etam']);
					$etdh = intval($_POST['new_schitem_etdh']);
					$etdm = intval($_POST['new_schitem_etdm']);
					$fleet->addScheduleItem($_POST['new_schitem_port'], $_POST['new_schitem_country'], $eta, $etah, $etam, $etd, $etdh, $etdm); 
				}
			break;
		}
	}
	
	public function runPostActions()
	{
		global $fleet;
		global $commander;
		switch ($_POST['action'])
		{
			case 'fleet_list':
				if(isset($fleet->ShipList))
				{
					/* Subitems list actions */
					if(count($_POST['ship_id']) > 0)
					{
						$i=0;
						foreach($_POST['ship_id'] as $id)
						{
							if($commander->formatText($_POST['ship_name'][$i]) != $fleet->ShipList[$id]->getName())
							{ 
								$fleet->ShipList[$id]->setName($commander->formatText($_POST['ship_name'][$i]));
								$fleet->ShipList[$id]->setDataChanged(true);
							}
							if($_POST['ship_identity_number'][$i] != $fleet->ShipList[$id]->getIdentityNumber())
							{ 
								$fleet->ShipList[$id]->setIdentityNumber($commander->formatInt($_POST['ship_identity_number'][$i]));
								$fleet->ShipList[$id]->setDataChanged(true);
							}
							$fleet->ShipList[$id]->update();
							$i++;
						}
						if(($_POST['activate'] > 0) && (isset($fleet->ShipList[$_POST['activate']])))
						{ 
							$fleet->ShipList[$_POST['activate']]->setActive(1);
							$commander->setTableActive($_POST['activate'], 'fleet_ship', 1); 
						}
						if(($_POST['deactivate'] > 0) && (isset($fleet->ShipList[$_POST['deactivate']])))
						{ 
							$fleet->ShipList[$_POST['deactivate']]->setActive(0);
							$commander->setTableActive($_POST['deactivate'], 'fleet_ship', 0);  
						}
						if(($_POST['ship_delete'] > 0) && (isset($fleet->ShipList[$_POST['ship_delete']])))
						{  $fleet->deleteShip($_POST['ship_delete']); }
						
						if($_POST['selected'])
						{
							foreach($_POST['selected'] as $id)
							{
								if(isset($fleet->ShipList[$id]))
								{
									if($_POST['activate'] == '-1')
									{
										$fleet->ShipList[$id]->setActive(1);
										$commander->setTableActive($id, 'fleet_ship', 1); 
									}
									if($_POST['deactivate'] == '-1')
									{
										$fleet->ShipList[$id]->setActive(0);
										$commander->setTableActive($id, 'fleet_ship', 0); 
									}
								}
							}
						}
					}
				}
			break;
			case 'editor_ship':
				if(isset($fleet->CurrentShip))
				{
					if(isset($fleet->CurrentShip->ScheduleItems))
					{
						if(count($_POST['schitem_id']) > 0)
						{	
							$i=0;
							foreach($_POST['schitem_id'] as $schItem)
							{
								if($_POST['schitem_id'][$i] == $fleet->CurrentShip->ScheduleItems[$schItem]->getId())
								{
									if($_POST['schitem_port'][$i] != $fleet->CurrentShip->ScheduleItems[$schItem]->getPort())
									{ 
										$fleet->CurrentShip->ScheduleItems[$schItem]->setPort($commander->formatText($_POST['schitem_port'][$i]));
										$fleet->CurrentShip->ScheduleItems[$schItem]->setDataChanged(true);
									}
									if($_POST['schitem_country'][$i] != $fleet->CurrentShip->ScheduleItems[$schItem]->getCountry())
									{ 
										$fleet->CurrentShip->ScheduleItems[$schItem]->setCountry(trim($_POST['schitem_country'][$i]));
										$fleet->CurrentShip->ScheduleItems[$schItem]->setDataChanged(true);
									}
									if($_POST['schitem_eta'][$i] != $fleet->CurrentShip->ScheduleItems[$schItem]->getEtaDate())
									{ 
										$fleet->CurrentShip->ScheduleItems[$schItem]->setEtaDate($_POST['schitem_eta'][$i]);
										$fleet->CurrentShip->ScheduleItems[$schItem]->changeEtaValues();
										$fleet->CurrentShip->ScheduleItems[$schItem]->setDataChanged(true);
									}
									if($_POST['schitem_etah'][$i] != $fleet->CurrentShip->ScheduleItems[$schItem]->getEtaHour())
									{ 
										$fleet->CurrentShip->ScheduleItems[$schItem]->setEtaHour($_POST['schitem_etah'][$i]);
										$fleet->CurrentShip->ScheduleItems[$schItem]->changeEtaValues();
										$fleet->CurrentShip->ScheduleItems[$schItem]->setDataChanged(true);
									}
									if($_POST['schitem_etam'][$i] != $fleet->CurrentShip->ScheduleItems[$schItem]->getEtaMinute())
									{ 
										$fleet->CurrentShip->ScheduleItems[$schItem]->setEtaMinute($_POST['schitem_etam'][$i]);
										$fleet->CurrentShip->ScheduleItems[$schItem]->changeEtaValues();
										$fleet->CurrentShip->ScheduleItems[$schItem]->setDataChanged(true);
									}
									if($_POST['schitem_etd'][$i] != $fleet->CurrentShip->ScheduleItems[$schItem]->getEtdDate())
									{ 
										$fleet->CurrentShip->ScheduleItems[$schItem]->setEtdDate($_POST['schitem_etd'][$i]);
										$fleet->CurrentShip->ScheduleItems[$schItem]->changeEtdValues();
										$fleet->CurrentShip->ScheduleItems[$schItem]->setDataChanged(true);
									}
									if($_POST['schitem_etdh'][$i] != $fleet->CurrentShip->ScheduleItems[$schItem]->getEtdHour())
									{ 
										$fleet->CurrentShip->ScheduleItems[$schItem]->setEtdHour($_POST['schitem_etdh'][$i]);
										$fleet->CurrentShip->ScheduleItems[$schItem]->changeEtdValues();
										$fleet->CurrentShip->ScheduleItems[$schItem]->setDataChanged(true);
									}
									if($_POST['schitem_etdm'][$i] != $fleet->CurrentShip->ScheduleItems[$schItem]->getEtdMinute())
									{ 
										$fleet->CurrentShip->ScheduleItems[$schItem]->setEtdMinute($_POST['schitem_etdm'][$i]);
										$fleet->CurrentShip->ScheduleItems[$schItem]->changeEtdValues();
										$fleet->CurrentShip->ScheduleItems[$schItem]->setDataChanged(true);
									}
									if($_POST['schitem_archived'][$i] != $fleet->CurrentShip->ScheduleItems[$schItem]->getArchived())
									{ 
										$fleet->CurrentShip->ScheduleItems[$schItem]->setArchived($_POST['schitem_archived'][$i]);
										$fleet->CurrentShip->ScheduleItems[$schItem]->setDataChanged(true);
									}
									if($_POST['archive'] == $fleet->CurrentShip->ScheduleItems[$schItem]->getId())
									{ 
										$fleet->CurrentShip->ScheduleItems[$schItem]->setArchived(1);
										$fleet->CurrentShip->ScheduleItems[$schItem]->setDataChanged(true);
									}
									if($_POST['unarchive'] == $fleet->CurrentShip->ScheduleItems[$schItem]->getId())
									{ 
										$fleet->CurrentShip->ScheduleItems[$schItem]->setArchived(0);
										$fleet->CurrentShip->ScheduleItems[$schItem]->setDataChanged(true);
									}
									$fleet->CurrentShip->ScheduleItems[$schItem]->update();
									if(($_POST['schitem_delete'] > 0) && ($_POST['schitem_delete'] == $fleet->CurrentShip->ScheduleItems[$schItem]->getId()))
									{ 
										if($fleet->deleteScheduleItem($schItem))
										{ unset($fleet->CurrentShip->ScheduleItems[$schItem]); }
									}
								}
								$i++;
							}
						}
					}
				}
			break;
		}
	}
	
	function getFleetMenuHTML()
	{
		global $fleet;
		global $commander;
		if(isset($fleet->ShipList))
		{
			$output .= '<ul>';
			foreach($fleet->ShipList as $ship)
			{
				$style[0] = ' style="background-image:url('.SYSICONS_URL.'treemenu/object_16.gif)" ';
				
				$class = '';
				if($_REQUEST['ship'] == $ship->getId())
				{ $class = 'open'; }
				
				$output .= 	'<li id="ship_'.$ship->getId().'" class="'.$class.' " '.$state.' rel="survey">';
				$output .= 		'<a '.$style[0].' href="?con='.$commander->Con.'&amp;ship='.$ship->getId().'">'.$ship->getName().'</a>';
				$output .= 	'</li>';	
			}
			$output .= '</ul>';
		}
		return $output;
	}
}
?>